#include "soc.h"
#include "stdlix.h"

int socket1,socket2;

int receive_from_node(char ipv6_addr[64],NodMessage *message)
{
    int len;
    const int opt = 1;
    int i=0;
    struct sockaddr_in6 addr,remote_addr,send_addr/*,tun_addr*/;
    int addr_len,remoteaddr_len;
//    char remoteipv6_addr[300];
    char msg[200];
//	printf("%u\n",sizeof(msg));

    NodMessage *node;
    //char REMOTEIP[32];
    //strcpy(REMOTEIP,argv[1]);
    addr_len=sizeof(struct sockaddr_in6);

 if(!socket1)
 {
    if((socket1=socket(AF_INET6,SOCK_DGRAM,0))<0)
    {
//        perror("error:");
        return 0;
    }
/*    else
    {
        printf("socket created ...\n");
        printf("socket id :%d \n",socket1);
        //printf("rmote ip : %s\n",REMOTEIP);
        printf("remote port :%d \n",REMOTEPORT);
    }*/
    bzero(&addr,sizeof(addr));
        addr.sin6_family=AF_INET6;
        addr.sin6_port=htons(REMOTEPORT);
        inet_pton(AF_INET6,ipv6_addr,&addr.sin6_addr);//IPV6 addr
    setsockopt(socket1,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
    if(bind(socket1,(struct sockaddr *)&addr,sizeof(addr))<0)
        {
//            perror("connect");
            return 0;
        }
/*        else
        {
            printf("bink ok .\n");
            //printf("local port : %d\n",LOCALPORT);
        }*/
 }

    bzero(&send_addr,sizeof(send_addr));
    send_addr.sin6_family=AF_INET6;
    send_addr.sin6_port=htons(3002);


    //bzero(&tun_addr,sizeof(tun_addr));
    //tun_addr.sin6_family=AF_INET6;
    //tun_addr.sin6_port=htons(3002);
    //inet_pton(AF_INET6,"aaaa::1",&tun_addr.sin6_addr);




 if(!socket2)
 {
    if((socket2=socket(AF_INET6,SOCK_DGRAM,0))<0)
    {
//        perror("error:");
        return 0;
    }

 }

    


   
        bzero(&remote_addr,sizeof(struct sockaddr_in6));
        remoteaddr_len=sizeof(struct sockaddr_in6);
        len=recvfrom(socket1,msg,sizeof(msg),0,(struct sockaddr *)&remote_addr,&remoteaddr_len);
//        printf("*****receive %d  %d  %d  %d  %d  *****\n",msg[0],msg[1],msg[2],msg[3],msg[4]);
        send_addr.sin6_addr=remote_addr.sin6_addr;
//        inet_ntop(AF_INET6, &remote_addr.sin6_addr, remoteipv6_addr,sizeof(remoteipv6_addr));

//        printf("%d:",i);
        i++;
//        printf("Received message : %s,ipv6 address:%s\n",msg,remoteipv6_addr);
        node=(NodMessage *)msg;
//	    printf("look%d",msg[0]);
        table[node->data[0]-1] = remote_addr.sin6_addr;
        if(msg[0]==NODE_BORN)
        {
		    int n;
//            strcpy(table[node->data[0]-1],remoteipv6_addr);
		    //table[node->data[0]-1] = remote_addr.sin6_addr;
            //n=sendto(socket1,msg,sizeof(msg),0,(struct sockaddr *)&send_addr,sizeof(send_addr));
//	        printf("recieve born\n");
		    n=sendto(socket1,msg,10,0,(struct sockaddr *)&send_addr,sizeof(send_addr));
			printf("end send ,receive success: %d\n",n);
		    if(n==-1)
			{
//				printf("chucuo");
//		    	perror("send error");
				return FALSE;
			}
/*			for(i=0;i<5;i++)
			printf("%d",msg[i]);
			printf("\n");
            printf("send to node\n");*/
            //复制message
            message->order=msg[0];
            //strcpy(message->data,&msg[1]);//或者加入一个链表之中
            message->data[0]=msg[1];
            message->data[1]=msg[2];
            message->data[2]=msg[3];
            message->data[3]=msg[4];
        }
        else if(msg[0]==DATA||msg[0]==NODE_HEART)
        {
            if(msg[0]==DATA)
            {
                if(msg[5]!=data_seq[msg[1]-1])
                {
                    message->order=msg[0];
                    //strcpy(message->data,&msg[1]);//或者加入一个链表之中
                    message->data[0]=msg[1];
                    message->data[1]=msg[2];
                    message->data[2]=msg[3];
                    message->data[3]=msg[4];
                    //printf("get message order : %d\n",message->order);
                    data_seq[msg[1]-1]=msg[5];
                }
                else
                {
                	//sendto(socket2,msg,sizeof(msg),0,(struct sockaddr *)&send_addr,addr_len);
                	return 0;
                }
            }
            else
            {
                if(msg[5]!=heart_seq[msg[1]-1])
                {
//                	printf("序列号=%d,%d\n",msg[5],heart_seq[msg[1]-1]);
                    message->order=msg[0];
                    message->data[0]=msg[1];
                    message->data[1]=msg[2];
                    message->data[2]=msg[3];
                    message->data[3]=msg[4];
                    //strcpy(message->data,&msg[1]);
//                    printf("get message order : %d\n",message->order);
                    heart_seq[msg[1]-1]=msg[5];
//                    printf("序列号=%d,%d\n",msg[5],heart_seq[msg[1]-1]);
                    
                }
                else
                {
//                	printf("receive 0\n");
                	sendto(socket1,msg,10,0,(struct sockaddr *)&send_addr,addr_len);
                	return 0;
                }
//                printf("******\nsend to %d",/*);*/
                sendto(socket1,msg,10,0,(struct sockaddr *)&send_addr,addr_len)/*)*/;
            }


//        }
        
            
        
    }
/*
        printf("get message order : %d\n",message->order);
        for(i = 0; i < 4; i++)
                				printf("get message [%d] is %d\n",i,message->data[i]);
*/

//        printf("message 3\\4 %d,%d",message->data[1],message->data[2]);
    return 1;

}


